package org.dromara.system.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.system.domain.bo.WbRelationshipsBo;
import org.dromara.system.domain.vo.WbRelationshipsVo;
import org.dromara.system.domain.WbRelationships;
import org.dromara.system.mapper.WbRelationshipsMapper;
import org.dromara.system.service.IWbRelationshipsService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 节点关系Service业务层处理
 *
 * @author Lion Li
 * @date 2024-09-22
 */
@RequiredArgsConstructor
@Service
public class WbRelationshipsServiceImpl implements IWbRelationshipsService {

    private final WbRelationshipsMapper baseMapper;

    /**
     * 查询节点关系
     */
    @Override
    public WbRelationshipsVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询节点关系列表
     */
    @Override
    public TableDataInfo<WbRelationshipsVo> queryPageList(WbRelationshipsBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WbRelationships> lqw = buildQueryWrapper(bo);
        Page<WbRelationshipsVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询节点关系列表
     */
    @Override
    public List<WbRelationshipsVo> queryList(WbRelationshipsBo bo) {
        LambdaQueryWrapper<WbRelationships> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<WbRelationships> buildQueryWrapper(WbRelationshipsBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WbRelationships> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getRelationshipId()), WbRelationships::getRelationshipId, bo.getRelationshipId());
        lqw.eq(StringUtils.isNotBlank(bo.getStartNode()), WbRelationships::getStartNode, bo.getStartNode());
        lqw.like(StringUtils.isNotBlank(bo.getStartNodeName()), WbRelationships::getStartNodeName, bo.getStartNodeName());
        lqw.eq(StringUtils.isNotBlank(bo.getStartNodeStatus()), WbRelationships::getStartNodeStatus, bo.getStartNodeStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getStartNodeLabels()), WbRelationships::getStartNodeLabels, bo.getStartNodeLabels());
        lqw.eq(StringUtils.isNotBlank(bo.getType()), WbRelationships::getType, bo.getType());
        lqw.eq(StringUtils.isNotBlank(bo.getEndNode()), WbRelationships::getEndNode, bo.getEndNode());
        lqw.like(StringUtils.isNotBlank(bo.getEndNodeName()), WbRelationships::getEndNodeName, bo.getEndNodeName());
        lqw.eq(StringUtils.isNotBlank(bo.getEndNodeStatus()), WbRelationships::getEndNodeStatus, bo.getEndNodeStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getEndNodeLabels()), WbRelationships::getEndNodeLabels, bo.getEndNodeLabels());
        lqw.eq(StringUtils.isNotBlank(bo.getPercent()), WbRelationships::getPercent, bo.getPercent());
        lqw.eq(StringUtils.isNotBlank(bo.getRelationshipLabels()), WbRelationships::getRelationshipLabels, bo.getRelationshipLabels());
        return lqw;
    }

    /**
     * 新增节点关系
     */
    @Override
    public Boolean insertByBo(WbRelationshipsBo bo) {
        WbRelationships add = MapstructUtils.convert(bo, WbRelationships.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改节点关系
     */
    @Override
    public Boolean updateByBo(WbRelationshipsBo bo) {
        WbRelationships update = MapstructUtils.convert(bo, WbRelationships.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(WbRelationships entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除节点关系
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
